package com.mysmartphone.views;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.mysmartphone.tools.Tools;
import com.mysmartphone.vistas.R;

public class SettingsFragment extends Fragment implements OnClickListener {

	TextView txtLanguage = null;
	ImageView flagSpain = null;
	ImageView flagUs = null;
	TextView txtCoin = null;
	CheckBox checkEuro = null;
	TextView txtEuro = null;
	CheckBox checkDollar = null;
	TextView txtDollar = null;
	String actualLanguage;
	String languageNext;
	String ES = "es";
	String EN = "en";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_settings, container,
				false);
		actualLanguage = Tools.getLanguage(getActivity().getBaseContext().getResources());
		
		txtLanguage = (TextView) rootView.findViewById(R.id.txtLanguage);
		flagSpain = (ImageView) rootView.findViewById(R.id.flagSpain);
		flagUs = (ImageView) rootView.findViewById(R.id.flagUs);
		
		txtCoin = (TextView) rootView.findViewById(R.id.txtCoin);
		checkEuro = (CheckBox) rootView.findViewById(R.id.checkEuro);
		txtEuro = (TextView) rootView.findViewById(R.id.txtEuro);
		
		loadFlag();
		
		//Obtener la moneda de base de datos
		checkEuro.setChecked(true);
		checkDollar = (CheckBox) rootView.findViewById(R.id.checkDollar);
		txtDollar = (TextView) rootView.findViewById(R.id.txtDollar);

		checkDollar.setOnClickListener(this);
		checkEuro.setOnClickListener(this);
		flagSpain.setOnClickListener(this);
		flagUs.setOnClickListener(this);

		return rootView;
	}

	private void loadFlag() {
		if(actualLanguage.equals(ES)){
			flagSpain.setImageResource(R.drawable.spain);
			flagUs.setImageResource(R.drawable.us_d);
		}else if(actualLanguage.equals(EN)){
			flagSpain.setImageResource(R.drawable.spain_d);
			flagUs.setImageResource(R.drawable.us);
		}else{
			flagSpain.setImageResource(R.drawable.spain_d);
			flagUs.setImageResource(R.drawable.us);
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.checkEuro) {
			checkEuro.setChecked(true);
			if (checkEuro.isChecked()) {
				checkDollar.setChecked(false);
			}
		}
		if (v.getId() == R.id.checkDollar) {
			checkDollar.setChecked(true);
			if (checkDollar.isChecked()) {
				checkEuro.setChecked(false);
			}
		}

		if (v.getId() == R.id.flagSpain) {
			languageNext = ES;
			if(!languageNext.equals(actualLanguage)){
				confirm();	
			}		}
		if (v.getId() == R.id.flagUs) {
			languageNext = EN;
			if(!languageNext.equals(actualLanguage)){
				confirm();	
			}
		}
	}

	private void confirm() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
	    builder.setMessage(getString(R.string.ask))
	       .setCancelable(false)
	       .setPositiveButton(getString(R.string.reboot), new DialogInterface.OnClickListener() {
	        
			public void onClick(DialogInterface dialog, int id) {
				if(languageNext.equals(ES)){
					flagUs.setImageResource(R.drawable.us_d);
					flagSpain.setImageResource(R.drawable.spain);
					Tools.setLanguage(getActivity().getBaseContext(),ES);
					Tools.resetApp(getActivity().getBaseContext());
				}else if(languageNext.equals(EN)){
					flagUs.setImageResource(R.drawable.us);
					flagSpain.setImageResource(R.drawable.spain_d);
					Tools.setLanguage(getActivity().getBaseContext(),EN);
					Tools.resetApp(getActivity().getBaseContext());
				}				
				
	           }
	       })
	       .setNegativeButton(getString(R.string.anotherTime), new DialogInterface.OnClickListener() {
	           public void onClick(DialogInterface dialog, int id) {
	               
	           }
	       });
	    AlertDialog alert = builder.create();
	    alert.show();
	}

}
